<?php
/**
 * 企动客服知识库模块微站定义
 *
 * @author yzm
 * @url http://bbs.we7.cc/
 */
session_start();
defined('IN_IA') or exit('Access Denied');

define('KB_PATH', IA_ROOT . '/addons/wherein_knowledge/');//项目绝对路径
define('KB_WEB', KB_PATH . 'application/web/');//项目后台绝对路径
define('KB_APP', KB_PATH . 'application/app/');//项目前端绝对路径

define('KB_WEB_RELATIVE', '../addons/wherein_knowledge/application/web/');//项目后台相对路径
define('KB_APP_RELATIVE', '../addons/wherein_knowledge/application/app/');//项目后台相对路径
define('KB_WEB_BACK_CSS', KB_WEB_RELATIVE . 'view/static/back_end/css');
define('KB_WEB_BACK_JS', KB_WEB_RELATIVE . 'view/static/back_end/js');
define('KB_WEB_BACK_IMG', KB_WEB_RELATIVE . 'view/static/back_end/img');

define('KB_WEB_FRONT_CSS', KB_WEB_RELATIVE . 'view/static/front_end/css');
define('KB_WEB_FRONT_JS', KB_WEB_RELATIVE . 'view/static/front_end/js');
define('KB_WEB_FRONT_IMG', KB_WEB_RELATIVE . 'view/static/front_end/img');

define('KB_APP_CSS', KB_APP_RELATIVE . 'view/static/css');
define('KB_APP_JS', KB_APP_RELATIVE . 'view/static/js');
define('KB_APP_IMG', KB_APP_RELATIVE . 'view/static/img');

define('QUESTION_IMPLODE', '##');
define("IMG_IMPLODE",'##'); //商品图集分隔符
define('NO_AVATAR', KB_WEB_BACK_IMG . '/noavatar_middle.gif');
define('UPLOAD_EXCEL', ATTACHMENT_ROOT . 'wherein_knowledge/excel/');//excel上传路径
define('UPLOAD_LOGO', ATTACHMENT_ROOT . 'wherein_knowledge/logo/');//logo上传路径
define('UPLOAD_ANSWER', ATTACHMENT_ROOT . 'wherein_knowledge/answer/');//answer上传路径

require_once KB_PATH . 'common/common.func.php';

class Wherein_knowledgeModuleSite extends WeModuleSite {

    public $order_status = array(
        0 => '待处理',
        1 => '已解决',
        2 => '已处理',
    );
    public $customer_status = array(
        1 => '未回复',
        2 => '回复待反馈',
        3 => '选择未解决',
        4 => '选择已解决',
    );

    /*
     * 所有请求到__call
     */
    public function __call($name, $arguments) {
        $isWeb = stripos($name, 'doWeb') === 0;
        $isMobile = stripos($name, 'doMobile') === 0;
        if ($isWeb || $isMobile) {
            $dir = IA_ROOT . '/addons/' . $this->modulename . '/application/';
            $dir .= $isWeb ? 'web/' : '';
            $dir .= $isMobile ? 'app/' : '';
            $file = $dir . 'index.php';
            if (file_exists($file)) {
                require $file;
                exit();
            }
        }
        trigger_error('访问的方法 ' . $name . ' 不存在.', 512);
    }

    /*
     * 自定义输出json
     * @param array $param
     * @param int $json_option default 0
     */
    public function outjson($param,$json_option=0){
        header('Content-Type:application/json; charset=utf-8');
        $data = json_encode($param,$json_option);
        exit($data);
    }

	public function doWebRules() {
		//这个操作被定义用来呈现 规则列表
	}

    public function doWebSetting() {
        //这个操作被定义用来呈现 管理中心导航菜单
        header('Location: ' . $this->createWebUrl('system', array('act' => 'index')));
    }

    public function doMobileWebservice() {
        global $_W;
        header("Location: " . $_W['siteroot'] . 'web/' . $this->createWebUrl('service', array('act' => 'index', 'uniacid' => $_W['uniacid'])));
        exit;
    }

    /*
     * 客户七天内未做出回应,工单自动转为已解决状态
     */
    public function checkOrderStatus() {
        global $_W;
        $time = time();
        //获取所有已处理的工单
        $order_list = pdo_fetchall("SELECT order_id,openid,nickname,add_time,expire_time,order_status,customer_status FROM " . tablename('wherein_knowledge_work_order') . " WHERE `order_status` = 2 AND `customer_status` = 2 AND `belong_to` = 0 AND `parent_id` = 0 AND `expire_time` < '" . $time . "' ");
        if (!empty($order_list)) {
            //如果回复后的七天时间小于当前时间
            $ids = return_array_value($order_list, 'order_id');

            pdo_query("UPDATE ".tablename('wherein_knowledge_work_order')." SET expire_time = :expire_time, order_status = :order_status AND customer_status=:customer_status AND unsolved_status=:unsolved_status AND is_read=:is_read WHERE uniacid = '" . $_W['uniacid'] . "' AND order_id IN(". implode(',', $ids) . ")", array(':expire_time' => '', ':order_status' => 1, ':customer_status' => 4, ':unsolved_status' => 2, ':is_read' => 0));
        }


    }

}